package leetcode;

import java.util.*;

public class Demo3 {

    //315. 计算右侧小于当前元素的个数
    int[] count;
    int[] index;    // 标记 当前元素的原始下标
    int[] tmpArray;
    int[] tmpIndex;
    public List<Integer> countSmaller(int[] nums) {
        int n = nums.length;
        count = new int[n];
        tmpArray = new int[n];
        index = new int[n];
        tmpIndex = new int[n];
        // 初始化 index 数组
        for(int i = 0; i < nums.length; i++) index[i] = i;

        mergeSort(nums, 0, nums.length - 1);
        List<Integer> ret = new ArrayList<>();
        for(int x : count) ret.add(x);
        return ret;
    }

    private void mergeSort(int[] arr, int left, int right) {
        if(left >= right) return;

        // 1. 将数组进行左右分区
        int mid = (left + right) / 2;
        mergeSort(arr, left, mid);
        mergeSort(arr, mid + 1, right);

        // 2. 处理一左一右的情况，并合并数组
        int cur1 = left, cur2 = mid + 1, k = left;
        while(cur1 <= mid && cur2 <= right) {   // 降序排序
            if(arr[cur1] <= arr[cur2]) {
                tmpIndex[k] = index[cur2];
                tmpArray[k++] = arr[cur2++];
            }
            else {
                count[index[cur1]] += right - cur2 + 1;     // 统计结果,重点重点重点
                tmpIndex[k] = index[cur1];
                tmpArray[k++] = arr[cur1++];
            }
        }
        while(cur1 <= mid) {
            tmpIndex[k] = index[cur1];
            tmpArray[k++] = arr[cur1++];
        }
        while(cur2 <= right) {
            tmpIndex[k] = index[cur2];
            tmpArray[k++] = arr[cur2++];
        }

        // 3. 将 tmpArray和tmpIndex 分别放回 arr和index
        while(left <= right) {
            index[left] = tmpIndex[left];
            arr[left] = tmpArray[left++];
        }
    }



    //493. 翻转对
    //int[] tmp;
    //    public int reversePairs(int[] nums) {
    //        tmp = new int[nums.length];
    //        return merge(nums, 0, nums.length - 1);
    //    }
    //
    //    private int merge(int[] nums, int left, int right) {
    //        if(left >= right) return 0;
    //
    //        // 1. 根据中间元素，将区间分为2部分， 并求出左右两个区间的翻转对
    //        int mid = (left + right) / 2, ret = 0;
    //        ret += merge(nums, left, mid);
    //        ret += merge(nums, mid + 1, right);
    //
    //        // 2. 处理一左一右的情况，计算翻转对
    //        for(int cur1 = left, cur2 = mid + 1; cur1 <= mid && cur2 <= right; ) {
    //            if(nums[cur1] / 2.0 <= nums[cur2]) cur2++;
    //            else {
    //                ret += right - cur2 + 1;
    //                cur1++;
    //            }
    //        }
    //
    //        int k = left, i = left, j = mid + 1;
    //        while(i <= mid && j <= right) {         // 数组降序的情况
    //            if(nums[i] >= nums[j]) tmp[k++] = nums[i++];
    //            else tmp[k++] = nums[j++];
    //        }
    //        while(i <= mid)  tmp[k++] = nums[i++];
    //        while(j <= right) tmp[k++] = nums[j++];
    //
    //        // 将原数组排序
    //        while(left <= right) nums[left] = tmp[left++];
    //        return ret;
    //
    //    }

}
